<script setup>
// import { ref, reactive, onMounted, watchEffect, watch } from "vue"; 
import { ref, reactive, onMounted } from "vue";
import axios from "@/utils/request";
// import { message } from "ant-design-vue";
// import { paramsA, paramsB } from "./ceshi";

import { cloneDeep } from "lodash";
import editRouteModal from "./modules/editRouteModal.vue";

const loading = ref(false);
const visible = ref(false); // 弹框开关属性
const params = reactive({});
const notRemind = ref(false); // 不再提醒
const bottleneckProcessList = ref([]);
const expand = ref([true, true, true]);
const programmeList = ref([
  { label: "合并工序", value: 1, disabled: true },
  { label: "调整站点", value: 2, disabled: true },
  { label: "启用备用站", value: 3, disabled: true },
]); //方案列表
const programmeClick = ref(1); // 方案选中
const planDetails = ref(null); // 方案详情
const websocketData = ref({}); // 接收到的数据

const editRouteModalRef = ref(null);

// 弹窗开启时操作
const open = async (val) => {
  console.log(val, "----!!!---");
  visible.value = true;
  websocketData.value = cloneDeep(val);

  bottleneckProcessList.value = websocketData.value?.block_station_list || [];
  dataHandle(websocketData.value?.readjustment);
  params.id = 1;
  //   const cloneDeep
};

// 方案数据处理
const dataHandle = (pam) => {
  if (pam?.adjust_road_map) {
    const handleParams = cloneDeep(pam);
    if (handleParams?.merge_process_dispatch) {
      programmeList.value[0].disabled = false;
      programmeList.value[0].params = handleParams.merge_process_dispatch;
      programmeList.value[0].dispatch_info =
        handleParams.merge_process_dispatch.dispatch_info;
    }
    if (handleParams?.spare_process_dispatch) {
      programmeList.value[1].disabled = false;
      programmeList.value[1].params = handleParams.spare_process_dispatch;
      programmeList.value[1].dispatch_info =
        handleParams.spare_process_dispatch.dispatch_info;
    }
    if (handleParams?.standby_station_dispatch) {
      programmeList.value[2].disabled = false;
      programmeList.value[2].params = handleParams.standby_station_dispatch;
      programmeList.value[2].dispatch_info =
        handleParams.standby_station_dispatch.dispatch_info;
    }
    planDetails.value =
      programmeList.value[programmeClick.value - 1].dispatch_info;
    console.log(programmeList.value);
  }
};
// 方案应用
const onConfirm = async () => {
  let params = programmeList.value[programmeClick.value - 1].params;
  console.log(params, "---被应用的方案-----");

  // console.log(paramsA, "---被应用的方案A-----");
  // console.log(paramsB, "----方案B-----");
  // let params = paramsB;

  const res = await axios.postAction(
    "/api/mo/mo-proc-roadmap/planning-in-production",
    params
  );
  console.log(res, "----!!!!-----");
  if (+res.code === 200) {
    // editRouteModalRef.value.open({ moId: res.data.moId, routeId: res.data.id });
    editRouteModalRef.value.open(res.data, params);
  }
};
// 忽略
const onCancel = () => {
  visible.value = false;
};

const programmeChange = (val) => {
  programmeClick.value = val;
  planDetails.value = programmeList.value[val - 1].dispatch_info;
};
onMounted(async () => {});

// watchEffect(() => {});

// // 流水线切换监听
// watch();
//   () => params.value.lineId,
//   async (newVal) => {
//     if (newVal) await handleLineChange();
//   },
//   { deep: true, immediate: true }
defineExpose({
  open,
});
</script>

<template>
  <a-modal
    v-model:visible="visible"
    title="产中提示"
    :footer="null"
    width="728px"
    centered
    :maskClosable="false"
  >
    <a-spin :spinning="loading">
      <div class="prompt-top">
        <div class="prompt-top-left">
          <div><img src="./icon/sliders.png" alt="#" /></div>
          <div class="prompt-top-left-text">瓶颈工序</div>
        </div>
        <div class="prompt-top-right">
          <div
            v-for="(item, index) in bottleneckProcessList"
            :key="index"
            class="item"
          >
            <div class="title">
              <span class="drop">·</span>工序{{ item?.process_seq }}:
              {{ item?.process_name
              }}<span class="retract" @click="expand[index] = !expand[index]">{{
                expand[index] ? "收起" : "展开"
              }}</span>
            </div>
            <div class="contant" v-show="expand[index]">
              <p>
                1、预计【{{ item?.predict_block_time }}秒】后，【{{
                  item?.line_number
                }}-{{ item?.station_id }}】会堵塞；
              </p>
            </div>
          </div>
        </div>
      </div>
      <div class="prompt-contant">
        <div class="prompt-contant-title">解决方案</div>
        <div class="prompt-contant-show">
          <div
            v-for="(item, index) in programmeList"
            :key="index"
            :class="
              item.disabled
                ? 'item item-disabled'
                : programmeClick == item.value
                ? 'item item-actived'
                : 'item'
            "
            @click="programmeChange(item.value)"
          >
            <p class="left-top">方案{{ index + 1 }}</p>
            <p>{{ item.label }}</p>
            <div class="actived-triangle"></div>
          </div>
        </div>
        <div class="prompt-contant-fa">
          <p v-if="programmeClick && programmeClick == 1 && planDetails">
            1、【{{
              planDetails?.merge_process[0]?.process_seq +
              "，" +
              planDetails?.merge_process[0]?.process_name
            }}】【{{
              planDetails?.merge_process[1]?.process_seq +
              "，" +
              planDetails?.merge_process[1]?.process_name
            }}】合并至【{{
              planDetails?.merge_process[0]?.process_seq +
              "，" +
              planDetails?.merge_process[0]?.process_name
            }}】
          </p>
          <p v-if="programmeClick && programmeClick == 2 && planDetails">
            1、【{{ planDetails?.dispatch_station }}号站】由【{{
              planDetails?.dispatch_process.process_seq +
              "，" +
              planDetails?.dispatch_process.process_name
            }}】调整至【{{
              planDetails?.scheduled_process[0]?.process_seq +
              "，" +
              planDetails?.scheduled_process[0]?.process_name
            }}】
          </p>
          <p v-if="programmeClick && programmeClick == 3 && planDetails">
            1、【{{
              planDetails?.scheduled_process[0]?.process_seq +
              "，" +
              planDetails?.scheduled_process[0]?.process_name
            }}】启用【{{ planDetails?.standby_station }}号站】存储站
          </p>
        </div>
      </div>
      <div class="prompt-bottom">
        <div>
          <a-checkbox v-model:checked="notRemind">今日不再提醒</a-checkbox>
        </div>
        <div>
          <a-button class="cancel" @click="onCancel"> 忽略 </a-button>
          <a-button type="primary" class="confirm" @click="onConfirm">
            应用
          </a-button>
        </div>
      </div>
    </a-spin>
  </a-modal>
  <editRouteModal ref="editRouteModalRef" @ok="onCancel"></editRouteModal>
</template>

<style scoped lang="less">
.prompt-top {
  height: 100px;
  background: #f5f6f8;
  display: flex;
  justify-content: left;

  .prompt-top-left {
    width: 120px;
    background: #f0f0f0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .prompt-top-left-text {
      font-size: 14px;
      font-weight: 600;
      color: #333333;
    }
  }

  .prompt-top-right {
    padding-left: 20px;
    width: calc(100% - 120px);
    overflow-y: scroll;
    .item {
      margin-top: 10px;

      .title {
        font-size: 14px;
        font-weight: 400;
        color: #333333;
        display: flex;
        align-items: center;
        margin-bottom: 3px;

        .drop {
          font-size: 15px;
          font-weight: 600;
          margin-right: 5px;
        }

        .retract {
          color: #3e8ff7;
          cursor: pointer;
          margin-left: 10px;
        }
      }

      .contant {
        padding-left: 10px;
        font-size: 12px;
        font-weight: 400;
        color: #666666;

        p {
          margin: 0px;
          line-height: 22px;
        }
      }
    }
  }
}
.prompt-contant {
  .prompt-contant-title {
    height: 22px;
    margin-top: 30px;
    margin-bottom: 15px;
    line-height: 22px;
    font-size: 14px;
    font-weight: 600;
    color: #333333;
  }
  .prompt-contant-show {
    display: flex;
    justify-content: space-between;
    .item {
      width: 32%;
      height: 80px;
      font-size: 16px;
      font-weight: bold;
      color: #333333;
      line-height: 80px;
      text-align: center;
      background: #ffffff;
      border-radius: 4px 4px 4px 4px;
      border: 1px solid #dddddd;
      position: relative;
      cursor: pointer;

      .left-top {
        position: absolute;
        left: 0;
        top: 0;
        width: 48px;
        height: 24px;
        background: #3e8ff7;
        border-radius: 4px 0px 4px 0px;
        font-size: 12px;
        color: #ffffff;
        line-height: 24px;
        text-align: center;
      }

      &.item-actived {
        background: #eff7ff;
        border: 1px solid #3e8ff7;

        .actived-triangle {
          position: absolute;
          left: 0;
          bottom: 0;
          height: 8px;
          width: 100%;
          background: #3e8ff7;
          border-radius: 0px 0px 4px 4px;

          &:before {
            content: "";
            width: 0px;
            height: 0px;
            border-top: 10px solid #3e8ff7;
            border-left: 10px solid transparent;
            border-right: 10px solid transparent;
            position: absolute;
            top: 8px;
            left: calc(50% - 10px);
          }
        }
      }

      &.item-disabled {
        color: #bcbcbc;
        cursor: not-allowed;
        pointer-events: none;

        .left-top {
          color: #bcbcbc;
          background: #f3f3f3;
        }
      }
    }
  }
  .prompt-contant-fa {
    margin-top: 20px;
    height: 150px;
    background: #ffffff;
    border-radius: 4px 4px 4px 4px;
    border: 1px solid #dddddd;
    padding: 20px;
  }
}
.prompt-bottom {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .cancel {
    margin-right: 10px;
  }
}
</style>
